﻿using DataAchive.Extend;
using DataAchive.Model.Excel.Export;
using NPOI.HSSF.Record;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;

namespace DataAchive.ExContr
{
    internal class ImgClassifyFunc
    {
        internal static void ClassfiyPicture(ImgClassifyConfig imgClassifyConfig, List<ColRowReport> colRowReports)
        {

            ClassfiyPicture_EL__Mark(imgClassifyConfig, colRowReports);//el el_mark el_origin 
            Thread.Sleep(1000);
            ClassfiyPicture_EL__Source(imgClassifyConfig, colRowReports);
            Thread.Sleep(1000);

            ClassfiyPicture_VI__Mark(imgClassifyConfig, colRowReports);
            Thread.Sleep(1000);
            ClassfiyPicture_VI__Source(imgClassifyConfig, colRowReports);
            Thread.Sleep(1000);

            ClassfiyPicture_VB__Mark(imgClassifyConfig, colRowReports);
            Thread.Sleep(1000);
            ClassfiyPicture_VB__Source(imgClassifyConfig, colRowReports);
            Thread.Sleep(1000);

        }

        internal static void ClassfiyPicture_EL__Source(ImgClassifyConfig imgClassifyConfig, List<ColRowReport> colRowReports)
        {
            ClassfiyPicture_Core(colRowReports, ElOrViOrVb_SourceDir: imgClassifyConfig.ElSourceDir??"", ElOrViOrVb_DesDir: imgClassifyConfig.ElDesDir??"", originOrMarkDir: "",
            overlayDay: imgClassifyConfig.OverDays, AiNGHumanOk: imgClassifyConfig.AiNGHumanOk, AiOkHumanNg: imgClassifyConfig.AiOkHumanNg, DefectEnum.EL);
        }

        internal static void ClassfiyPicture_EL__Mark(ImgClassifyConfig imgClassifyConfig, List<ColRowReport> colRowReports)
        {
            ClassfiyPicture_Core(colRowReports, ElOrViOrVb_SourceDir: imgClassifyConfig.ElSourceDir ?? "", ElOrViOrVb_DesDir: imgClassifyConfig.ElDesDir??"", originOrMarkDir: imgClassifyConfig.MarkDir,
             overlayDay: imgClassifyConfig.OverDays, AiNGHumanOk: imgClassifyConfig.AiNGHumanOk, AiOkHumanNg: imgClassifyConfig.AiOkHumanNg, DefectEnum.EL);
            //if (!string.IsNullOrWhiteSpace(imgClassifyConfig.ElSourceDir))
            //{
            //    if (!Directory.Exists(imgClassifyConfig.ElSourceDir))
            //    {
            //        SoftCommon.Log.Write($"{imgClassifyConfig.ElSourceDir}不存在,请确认！");
            //        return;
            //    }
            //    //数据每一条 执行
            //    //mark or source
            //    var markPath = Path.Combine(imgClassifyConfig.ElSourceDir, imgClassifyConfig.MarkDir);
            //    if (!Directory.Exists(markPath))
            //    {
            //        SoftCommon.Log.Write($"{markPath}不存在,请确认！");
            //        return;
            //    }
            //    //目标_M  无_M
            //    var desPath = Path.Combine(imgClassifyConfig.ElDesDir, imgClassifyConfig.MarkDir);
            //    if (!Directory.Exists(desPath))
            //    {
            //        Directory.CreateDirectory(desPath);
            //    }


            //    var allDirecties = Directory.GetDirectories(markPath);

            //    foreach (var everyRow in colRowReports)
            //    {

            //        foreach (var direcory in allDirecties)
            //        {
            //            try
            //            {
            //                var dateName = Path.GetFileName(direcory);
            //                var isF = DateTime.TryParse(dateName, out var result);
            //                if (!isF)
            //                {
            //                    SoftCommon.Log.Write($"文件夹{direcory}非日期格式，跳过！");
            //                    continue;
            //                }
            //                if (result < DateTime.Now.Date.AddDays(-imgClassifyConfig.OverDays))
            //                {
            //                    SoftCommon.Log.Write($"文件夹{direcory}在{imgClassifyConfig.OverDays}天前，跳过！");
            //                    continue;
            //                }
            //                SoftCommon.Log.Write($"文件夹{result}。");

            //                //日期
            //                var desinationPath = Path.Combine(desPath, dateName);
            //                if (!Directory.Exists(desinationPath))
            //                {
            //                    Directory.CreateDirectory(desinationPath);
            //                }
            //                desinationPath = Path.Combine(desPath, dateName, imgClassifyConfig.AiNGHumanOk);
            //                if (!Directory.Exists(desinationPath))
            //                {
            //                    Directory.CreateDirectory(desinationPath);
            //                }
            //                desinationPath = Path.Combine(desPath, dateName, imgClassifyConfig.AiOkHumanNg);
            //                if (!Directory.Exists(desinationPath))
            //                {
            //                    Directory.CreateDirectory(desinationPath);
            //                }
            //                //获取日期下所有文件
            //                var allDateFiles  = Directory.EnumerateFiles(direcory,"*",SearchOption.AllDirectories).Where(file => Path.GetFileName(file).Contains(everyRow.LotId));

            //                //AiNGHumanOk
            //                //AiOkHumanNg
            //                //缺陷
            //                //
            //                if (everyRow.ElAiDetectResult.IndexOf("NG", StringComparison.OrdinalIgnoreCase) > -1&&
            //                    everyRow.ElHumanDetectResult.IndexOf("NG", StringComparison.OrdinalIgnoreCase)==-1)
            //                {
            //                    var item_el = everyRow.ElAiDetectInfo.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);                           
            //                    foreach (var elFault in item_el)
            //                    {
            //                        var fault = elFault.Trim();
            //                        if (AiDefectStaticRRR.IsNumeric(fault))
            //                        {
            //                            SoftCommon.Log.Write($"{elFault} continue;");
            //                            continue;
            //                        }
            //                        desinationPath = Path.Combine(desPath, dateName, imgClassifyConfig.AiNGHumanOk, fault);
            //                        if (!Directory.Exists(desinationPath))
            //                        {
            //                            Directory.CreateDirectory(desinationPath);
            //                        }
            //                        //* 重复跳过
            //                        foreach (var file in allDateFiles) 
            //                        {
            //                            try
            //                            {
            //                                SoftCommon.Log.Write($"复制文件{file}到{desinationPath}开始");
            //                                File.Copy(file, Path.Combine(desinationPath, Path.GetFileName(file)), false);
            //                                SoftCommon.Log.Write($"复制文件{file}到{desinationPath}结束!");
            //                            }
            //                            catch (Exception ex)
            //                            {

            //                                SoftCommon.Log.Write(ex.Message);
            //                                continue;
            //                            }


            //                        }

            //                    }
            //                }
            //                else if (everyRow.ElAiDetectResult.IndexOf("OK", StringComparison.OrdinalIgnoreCase) > -1 &&
            //                    everyRow.ElHumanDetectResult.IndexOf("OK", StringComparison.OrdinalIgnoreCase) == -1)
            //                {
            //                    var item_el = everyRow.ElAiDetectInfo.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            //                    foreach (var elFault in item_el)
            //                    {
            //                        var fault = elFault.Trim();
            //                        if (AiDefectStaticRRR.IsNumeric(fault))
            //                        {
            //                            SoftCommon.Log.Write($"{elFault} continue;");
            //                            continue;
            //                        }
            //                        desinationPath = Path.Combine(desPath, dateName, imgClassifyConfig.AiOkHumanNg, fault);
            //                        if (!Directory.Exists(desinationPath))
            //                        {
            //                            Directory.CreateDirectory(desinationPath);
            //                        }
            //                        //* 重复跳过
            //                        foreach (var file in allDateFiles)
            //                        {
            //                            try
            //                            {
            //                                SoftCommon.Log.Write($"复制文件{file}到{desinationPath}开始");
            //                                File.Copy(file, Path.Combine(desinationPath, Path.GetFileName(file)), false);
            //                                SoftCommon.Log.Write($"复制文件{file}到{desinationPath}结束!");
            //                            }
            //                            catch (Exception ex)
            //                            {

            //                                SoftCommon.Log.Write(ex.Message);
            //                                continue;
            //                            }


            //                        }

            //                    }
            //                }

            //            }
            //            catch (Exception ex)
            //            {

            //                SoftCommon.Log.Write(ex.Message);

            //            }

            //        }
            //    }

            //}
            //else
            //{
            //    SoftCommon.Log.Write("imgClassifyConfig.ElSourceDir 为空");
            //}
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="colRowReports"></param>
        /// <param name="ElOrViOrVb_SourceDir"></param>
        /// <param name="ElOrViOrVb_DesDir">目的地址</param>
        /// <param name="originOrMarkDir">原图为"",</param>
        internal static void ClassfiyPicture_Core(List<ColRowReport> colRowReports,string ElOrViOrVb_SourceDir,string ElOrViOrVb_DesDir,string originOrMarkDir,
            int overlayDay,string AiNGHumanOk,string AiOkHumanNg, DefectEnum defectEnum )
        {
            if (!string.IsNullOrWhiteSpace(ElOrViOrVb_SourceDir))
            {
                if (string.IsNullOrWhiteSpace(ElOrViOrVb_SourceDir)||!Directory.Exists(ElOrViOrVb_SourceDir))
                {
                    SoftCommon.Log.Write($"{ElOrViOrVb_SourceDir}不存在,请确认！");
                    return;
                }
                //数据每一条 执行
                //mark or source
                var markPath = Path.Combine(ElOrViOrVb_SourceDir, originOrMarkDir);
                if (!Directory.Exists(markPath))
                {
                    SoftCommon.Log.Write($"{markPath}不存在,请确认！");
                    return;
                }
                //目标_M  无_M
                var desPath = Path.Combine(ElOrViOrVb_DesDir, originOrMarkDir);
                if (!Directory.Exists(desPath))
                {
                    Directory.CreateDirectory(desPath);
                }


                var allDirecties = Directory.GetDirectories(markPath);

                foreach (var everyRow in colRowReports)
                {

                    foreach (var direcory in allDirecties)
                    {
                        try
                        {
                            var dateName = Path.GetFileName(direcory);
                            var isF = DateTime.TryParse(dateName, out var result);
                            if (!isF)
                            {
                                SoftCommon.Log.Write($"文件夹{direcory}非日期格式，跳过！");
                                continue;
                            }
                            if (result < DateTime.Now.Date.AddDays(-overlayDay))
                            {
                                SoftCommon.Log.Write($"文件夹{direcory}在{overlayDay}天前，跳过！");
                                continue;
                            }
                            SoftCommon.Log.Write($"文件夹{result}。");

                            //日期
                            var desinationPath = Path.Combine(desPath, dateName);
                            if (!Directory.Exists(desinationPath))
                            {
                                Directory.CreateDirectory(desinationPath);
                            }
                            desinationPath = Path.Combine(desPath, dateName, AiNGHumanOk);
                            if (!Directory.Exists(desinationPath))
                            {
                                Directory.CreateDirectory(desinationPath);
                            }
                            desinationPath = Path.Combine(desPath, dateName,AiOkHumanNg);
                            if (!Directory.Exists(desinationPath))
                            {
                                Directory.CreateDirectory(desinationPath);
                            }
                            //获取日期下所有文件
                            var allDateFiles = Directory.EnumerateFiles(direcory, "*", SearchOption.AllDirectories).Where(file => Path.GetFileName(file).Contains(everyRow.LotId));

                            //AiNGHumanOk
                            //AiOkHumanNg
                            //缺陷
                            //
                            if(defectEnum== DefectEnum.EL)
                            {
                                if (everyRow.ElAiDetectResult.IndexOf("NG", StringComparison.OrdinalIgnoreCase) > -1 &&
                                everyRow.ElHumanDetectResult.IndexOf("NG", StringComparison.OrdinalIgnoreCase) == -1)
                                {
                                    var item_el = everyRow.ElAiDetectInfo.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                                    foreach (var elFault in item_el)
                                    {
                                        var fault = elFault.Trim();
                                        if (AiDefectStaticRRR.IsNumeric(fault))
                                        {
                                            SoftCommon.Log.Write($"{elFault} continue;");
                                            continue;
                                        }
                                        desinationPath = Path.Combine(desPath, dateName, AiNGHumanOk, fault);
                                        if (!Directory.Exists(desinationPath))
                                        {
                                            Directory.CreateDirectory(desinationPath);
                                        }
                                        //* 重复跳过
                                        foreach (var file in allDateFiles)
                                        {
                                            try
                                            {
                                                SoftCommon.Log.Write($"复制文件{file}到{desinationPath}开始");
                                                File.Copy(file, Path.Combine(desinationPath, Path.GetFileName(file)), false);
                                                SoftCommon.Log.Write($"复制文件{file}到{desinationPath}结束!");
                                                Thread.Sleep(1500);
                                            }
                                            catch (Exception ex)
                                            {

                                                SoftCommon.Log.Write(ex.Message);
                                                continue;
                                            }


                                        }

                                    }
                                }
                                else if (everyRow.ElAiDetectResult.IndexOf("OK", StringComparison.OrdinalIgnoreCase) > -1 &&
                                    everyRow.ElHumanDetectResult.IndexOf("OK", StringComparison.OrdinalIgnoreCase) == -1)
                                {
                                    var item_el = everyRow.ElAiDetectInfo.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                                    foreach (var elFault in item_el)
                                    {
                                        var fault = elFault.Trim();
                                        if (AiDefectStaticRRR.IsNumeric(fault))
                                        {
                                            SoftCommon.Log.Write($"{elFault} continue;");
                                            continue;
                                        }
                                        desinationPath = Path.Combine(desPath, dateName, AiOkHumanNg, fault);
                                        if (!Directory.Exists(desinationPath))
                                        {
                                            Directory.CreateDirectory(desinationPath);
                                        }
                                        //* 重复跳过
                                        foreach (var file in allDateFiles)
                                        {
                                            try
                                            {
                                                SoftCommon.Log.Write($"复制文件{file}到{desinationPath}开始");
                                                File.Copy(file, Path.Combine(desinationPath, Path.GetFileName(file)), false);
                                                SoftCommon.Log.Write($"复制文件{file}到{desinationPath}结束!");
                                                Thread.Sleep(1500);
                                            }
                                            catch (Exception ex)
                                            {

                                                SoftCommon.Log.Write(ex.Message);
                                                continue;
                                            }


                                        }

                                    }
                                }
                            }
                            else if(defectEnum== DefectEnum.VI)
                            {
                                if (everyRow.VIAiDetectResult.IndexOf("NG", StringComparison.OrdinalIgnoreCase) > -1 &&
                                everyRow.VIHumanDetectResult.IndexOf("NG", StringComparison.OrdinalIgnoreCase) == -1)
                                {
                                    var item_vi = everyRow.ViAiDetectInfo.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                                    foreach (var viFault in item_vi)
                                    {
                                        var fault = viFault.Trim();
                                        if (AiDefectStaticRRR.IsNumeric(fault))
                                        {
                                            SoftCommon.Log.Write($"{viFault} continue;");
                                            continue;
                                        }
                                        desinationPath = Path.Combine(desPath, dateName, AiNGHumanOk, fault);
                                        if (!Directory.Exists(desinationPath))
                                        {
                                            Directory.CreateDirectory(desinationPath);
                                        }
                                        //* 重复跳过
                                        foreach (var file in allDateFiles)
                                        {
                                            try
                                            {
                                                SoftCommon.Log.Write($"复制文件{file}到{desinationPath}开始");
                                                File.Copy(file, Path.Combine(desinationPath, Path.GetFileName(file)), false);
                                                SoftCommon.Log.Write($"复制文件{file}到{desinationPath}结束!");
                                                Thread.Sleep(1500);
                                            }
                                            catch (Exception ex)
                                            {

                                                SoftCommon.Log.Write(ex.Message);
                                                continue;
                                            }


                                        }

                                    }
                                }
                                else if (everyRow.VIAiDetectResult.IndexOf("OK", StringComparison.OrdinalIgnoreCase) > -1 &&
                                    everyRow.VIHumanDetectResult.IndexOf("OK", StringComparison.OrdinalIgnoreCase) == -1)
                                {
                                    var item_vi = everyRow.ViAiDetectInfo.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                                    foreach (var viFault in item_vi)
                                    {
                                        var fault = viFault.Trim();
                                        if (AiDefectStaticRRR.IsNumeric(fault))
                                        {
                                            SoftCommon.Log.Write($"{viFault} continue;");
                                            continue;
                                        }
                                        desinationPath = Path.Combine(desPath, dateName, AiOkHumanNg, fault);
                                        if (!Directory.Exists(desinationPath))
                                        {
                                            Directory.CreateDirectory(desinationPath);
                                        }
                                        //* 重复跳过
                                        foreach (var file in allDateFiles)
                                        {
                                            try
                                            {
                                                SoftCommon.Log.Write($"复制文件{file}到{desinationPath}开始");
                                                File.Copy(file, Path.Combine(desinationPath, Path.GetFileName(file)), false);
                                                SoftCommon.Log.Write($"复制文件{file}到{desinationPath}结束!");
                                                Thread.Sleep(1500);
                                            }
                                            catch (Exception ex)
                                            {

                                                SoftCommon.Log.Write(ex.Message);
                                                continue;
                                            }


                                        }

                                    }
                                }
                            }
                            else if(defectEnum == DefectEnum.VB)
                            {
                                if (everyRow.VBAiDetectResult.IndexOf("NG", StringComparison.OrdinalIgnoreCase) > -1 &&
                               everyRow.VBHumanDetectResult.IndexOf("NG", StringComparison.OrdinalIgnoreCase) == -1)
                                {
                                    var item_vb = everyRow.VBAiDetectInfo.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                                    foreach (var vbFault in item_vb)
                                    {
                                        var fault = vbFault.Trim();
                                        if (AiDefectStaticRRR.IsNumeric(fault))
                                        {
                                            SoftCommon.Log.Write($"{vbFault} continue;");
                                            continue;
                                        }
                                        desinationPath = Path.Combine(desPath, dateName, AiNGHumanOk, fault);
                                        if (!Directory.Exists(desinationPath))
                                        {
                                            Directory.CreateDirectory(desinationPath);
                                        }
                                        //* 重复跳过
                                        foreach (var file in allDateFiles)
                                        {
                                            try
                                            {
                                                SoftCommon.Log.Write($"复制文件{file}到{desinationPath}开始");
                                                File.Copy(file, Path.Combine(desinationPath, Path.GetFileName(file)), false);
                                                SoftCommon.Log.Write($"复制文件{file}到{desinationPath}结束!");
                                                Thread.Sleep(1500);
                                            }
                                            catch (Exception ex)
                                            {

                                                SoftCommon.Log.Write(ex.Message);
                                                continue;
                                            }


                                        }

                                    }
                                }
                                else if (everyRow.VBAiDetectResult.IndexOf("OK", StringComparison.OrdinalIgnoreCase) > -1 &&
                                    everyRow.VBHumanDetectResult.IndexOf("OK", StringComparison.OrdinalIgnoreCase) == -1)
                                {
                                    var item_vb = everyRow.VBAiDetectInfo.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                                    foreach (var vbFault in item_vb)
                                    {
                                        var fault = vbFault.Trim();
                                        if (AiDefectStaticRRR.IsNumeric(fault))
                                        {
                                            SoftCommon.Log.Write($"{vbFault} continue;");
                                            continue;
                                        }
                                        desinationPath = Path.Combine(desPath, dateName, AiOkHumanNg, fault);
                                        if (!Directory.Exists(desinationPath))
                                        {
                                            Directory.CreateDirectory(desinationPath);
                                        }
                                        //* 重复跳过
                                        foreach (var file in allDateFiles)
                                        {
                                            try
                                            {
                                                SoftCommon.Log.Write($"复制文件{file}到{desinationPath}开始");
                                                File.Copy(file, Path.Combine(desinationPath, Path.GetFileName(file)), false);
                                                SoftCommon.Log.Write($"复制文件{file}到{desinationPath}结束!");
                                                Thread.Sleep(1500);
                                            }
                                            catch (Exception ex)
                                            {

                                                SoftCommon.Log.Write(ex.Message);
                                                continue;
                                            }


                                        }

                                    }
                                }
                            }
                            

                        }
                        catch (Exception ex)
                        {

                            SoftCommon.Log.Write(ex.Message);

                        }

                    }
                }

            }
            else
            {
                SoftCommon.Log.Write("imgClassifyConfig.ElSourceDir 为空");
            }
        }
        internal static void ClassfiyPicture_VI__Source(ImgClassifyConfig imgClassifyConfig, List<ColRowReport> colRowReports)
        {
            ClassfiyPicture_Core(colRowReports, ElOrViOrVb_SourceDir: imgClassifyConfig.ViSourceDir ?? "", ElOrViOrVb_DesDir: imgClassifyConfig.ViDesDir??"", originOrMarkDir: "",
                overlayDay: imgClassifyConfig.OverDays, AiNGHumanOk: imgClassifyConfig.AiNGHumanOk, AiOkHumanNg: imgClassifyConfig.AiOkHumanNg,DefectEnum.VI);
        }

        internal static void ClassfiyPicture_VI__Mark(ImgClassifyConfig imgClassifyConfig, List<ColRowReport> colRowReports)
        {
            ClassfiyPicture_Core(colRowReports, ElOrViOrVb_SourceDir: imgClassifyConfig.ViSourceDir ?? "", ElOrViOrVb_DesDir: imgClassifyConfig.ViDesDir ?? "", originOrMarkDir: imgClassifyConfig.MarkDir,
              overlayDay: imgClassifyConfig.OverDays, AiNGHumanOk: imgClassifyConfig.AiNGHumanOk, AiOkHumanNg: imgClassifyConfig.AiOkHumanNg, DefectEnum.VI);
        }
        internal static void ClassfiyPicture_VB__Source(ImgClassifyConfig imgClassifyConfig, List<ColRowReport> colRowReports)
        {
            ClassfiyPicture_Core(colRowReports, ElOrViOrVb_SourceDir: imgClassifyConfig.VbSourceDir ?? "", ElOrViOrVb_DesDir: imgClassifyConfig.VbDesDir??"", originOrMarkDir: "",
               overlayDay: imgClassifyConfig.OverDays, AiNGHumanOk: imgClassifyConfig.AiNGHumanOk, AiOkHumanNg: imgClassifyConfig.AiOkHumanNg,DefectEnum.VB);
        }
        internal static void ClassfiyPicture_VB__Mark(ImgClassifyConfig imgClassifyConfig, List<ColRowReport> colRowReports)
        {
            ClassfiyPicture_Core(colRowReports, ElOrViOrVb_SourceDir: imgClassifyConfig.VbSourceDir ?? "", ElOrViOrVb_DesDir: imgClassifyConfig.VbDesDir ?? "", originOrMarkDir: imgClassifyConfig.MarkDir,
               overlayDay: imgClassifyConfig.OverDays, AiNGHumanOk: imgClassifyConfig.AiNGHumanOk, AiOkHumanNg: imgClassifyConfig.AiOkHumanNg, DefectEnum.VB);
        }
    }
}
